# TODO: Create automatic package installation for students


## Settings #################################################################################################

# Global chunk settings
# Load packages

library(here)
library(conflicted)
library(httr)
library(rtweet)
library(quantmod)
library(Quandl)
library(pins)
library(tidyverse)
library(lubridate)
library(tsbox)
library(DT)
library(plotly)
library(wordcloud2)
library(RColorBrewer)
library(bookdown)

# Conflicted: preferences in case of conflict

conflict_prefer("here", "here")
conflict_prefer("filter", "dplyr")
conflict_prefer("lag", "dplyr")
conflict_prefer("select", "dplyr")

1 Plotting Settings

# Plotting settings

theme_set(theme_light())

# Color settings

palette(brewer.pal(n = 11, name = "RdYlGn"))

color_primary <- palette()[10]

2 Data Input

Download Tesla stock data (ticker = “TSLA”) from Yahoo Finance by using the quantmod package

getSymbols(Symbols = "TSLA",
           src     = "yahoo",
           verbose = T)
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## downloading  TSLA .....
## 
## done.
## [1] "TSLA"

Transform Tesla stock data into tibble with tsbox package and rename columns

df_Tesla_stock_data <- TSLA %>%
    ts_tbl() %>%
    ts_wide() %>%
    rename(Date     = time,
           Open     = TSLA.Open,
           High     = TSLA.High,
           Low      = TSLA.Low,
           Close    = TSLA.Close,
           Volume   = TSLA.Volume,
           Adjusted = TSLA.Adjusted)

# Get tweets data from Elon Musk's Twitter account (only most recent 3'212 tweets per user are available)

df_tweets_elon_musk <- get_timeline("ElonMusk", n = 5000)

3 Time Series Plot: Stock Price

# TODO: Create beautiful default theme

Create time series plot of Tesla stock price

# TODO: Build chart step by step
#       Add main events as points or lines, add labels, make lines thicker
#       Maybe add moving average

p_Tesla_stock_data <- df_Tesla_stock_data %>%
    ggplot(aes(x = Date, y = Adjusted)) +  # Close
    geom_point(col = palette()[11]) +
    geom_line(col = color_primary) +
    geom_smooth(col = palette()[2]) +
    labs(title = "Tesla Stock Price - Rising Higher and Higher...",
         y     = "Close (Adjusted)") +
    scale_x_date(date_breaks = "1 year",
                 date_labels = "%Y") +
    scale_y_continuous(labels = scales::dollar)

p_Tesla_stock_data <- p_Tesla_stock_data %>%
    ggplotly()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
p_Tesla_stock_data

4 Bar Plot: Stock Volume

Create bar plot of TSLA stock volume

p_bar_Tesla_stock_volume <- df_Tesla_stock_data %>%
    ggplot(aes(x = Date, y = Volume)) +
    geom_col(col = color_primary) +
    labs(title = "Tesla Trading Volume - ...While Trading Volume Remained Constant Over Time") +
    scale_x_date(date_breaks = "1 year",
                 date_labels = "%Y") +
    scale_y_continuous(labels = scales::dollar)

p_bar_Tesla_stock_volume <- p_bar_Tesla_stock_volume %>%
    ggplotly()

p_bar_Tesla_stock_volume

Put them together

subplot(p_Tesla_stock_data,
        p_bar_Tesla_stock_volume,
        nrows  = 2,
        shareX = T)

5 Bar Chart Plot: Elon Musk’s Tweets

# Compute number of Elon Musk's tweets per day

df_tweets_elon_musk_per_day <- df_tweets_elon_musk %>%
    mutate(Date = as.Date(created_at)) %>%
    group_by(Date) %>%
    summarise(TweetsN = n())

summary(df_tweets_elon_musk_per_day)
##       Date               TweetsN     
##  Min.   :2019-08-11   Min.   : 1.00  
##  1st Qu.:2019-11-04   1st Qu.: 4.00  
##  Median :2020-01-30   Median : 8.00  
##  Mean   :2020-01-29   Mean   :10.29  
##  3rd Qu.:2020-04-23   3rd Qu.:14.00  
##  Max.   :2020-07-15   Max.   :57.00
# Create a bar chart
# TODO: Set own color scale

p_bar_tweets_elon_musk <- df_tweets_elon_musk_per_day %>%
    ggplot(aes(x = Date, y = TweetsN, fill = TweetsN)) +
    geom_col() +
    scale_x_date(date_breaks = "1 month",
                 date_labels = "%Y %b") +
    scale_y_continuous(breaks = seq(0, 60, 10)) +
    labs(title = "Tweets by Elon Musk",
         x     = "Month",
         y     = "Number of Tweets") +
    scale_fill_binned(type = "viridis")

p_bar_tweets_elon_musk <- p_bar_tweets_elon_musk %>%
    ggplotly()

subplot(p_Tesla_stock_data,
        p_bar_tweets_elon_musk,
        nrows  = 2,
        shareX = T)
# Get Tesla tweets

df_tweets_elon_musk_Tesla <- df_tweets_elon_musk %>%
    filter(str_detect(text, pattern = "Tesla"))

6 Wordcloud Plot: Elon Musk’s Tweets

# TODO

# wordcloud2()

7 Candelstick Chart Plot: Stock Price

# OHLC

# TODO: Add nicer colors

p_LC <- df_Tesla_stock_data %>%
    ggplot(aes(x = Date, y = Adjusted)) +
    geom_line(size = 1) +
    geom_line(aes(y = Low),
              col      = palette()[2],
              linetype = "dashed") +
    geom_line(aes(y = High),
              col      = palette()[11],
              linetype = "dashed") +
    geom_ribbon(aes(ymin  = Low,
                    ymax  = High),
                alpha = 0.4) +
    labs(title = "Tesla Trading Volume - ...While Trading Volume Remained Constant Over Time") +
    scale_x_date(date_breaks = "1 year",
                 date_labels = "%Y") +
    scale_y_continuous(labels = scales::dollar)

p_LC %>%
    ggplotly()

8 Histogram Plot: Stock Returns